package com.theone.module.demo.controller.admin.testone;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.theone.framework.common.pojo.PageResult;
import com.theone.framework.common.pojo.CommonResult;
import static com.theone.framework.common.pojo.CommonResult.success;

import com.theone.framework.excel.core.util.ExcelUtils;

import com.theone.framework.operatelog.core.annotations.OperateLog;
import static com.theone.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.theone.module.demo.controller.admin.testone.vo.*;
import com.theone.module.demo.dal.dataobject.testone.TestOneDO;
import com.theone.module.demo.convert.testone.TestOneConvert;
import com.theone.module.demo.service.testone.TestOneService;

@Api(tags = "管理后台 - 第一个测试")
@RestController
@RequestMapping("/demo/test-one")
@Validated
public class TestOneController {

    @Resource
    private TestOneService testOneService;

    @PostMapping("/create")
    @ApiOperation("创建第一个测试")
    @PreAuthorize("@ss.hasPermission('demo:test-one:create')")
    public CommonResult<Long> createTestOne(@Valid @RequestBody TestOneCreateReqVO createReqVO) {
        return success(testOneService.createTestOne(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新第一个测试")
    @PreAuthorize("@ss.hasPermission('demo:test-one:update')")
    public CommonResult<Boolean> updateTestOne(@Valid @RequestBody TestOneUpdateReqVO updateReqVO) {
        testOneService.updateTestOne(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除第一个测试")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('demo:test-one:delete')")
    public CommonResult<Boolean> deleteTestOne(@RequestParam("id") Long id) {
        testOneService.deleteTestOne(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得第一个测试")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('demo:test-one:query')")
    public CommonResult<TestOneRespVO> getTestOne(@RequestParam("id") Long id) {
        TestOneDO testOne = testOneService.getTestOne(id);
        return success(TestOneConvert.INSTANCE.convert(testOne));
    }

    @GetMapping("/list")
    @ApiOperation("获得第一个测试列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('demo:test-one:query')")
    public CommonResult<List<TestOneRespVO>> getTestOneList(@RequestParam("ids") Collection<Long> ids) {
        List<TestOneDO> list = testOneService.getTestOneList(ids);
        return success(TestOneConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得第一个测试分页")
    @PreAuthorize("@ss.hasPermission('demo:test-one:query')")
    public CommonResult<PageResult<TestOneRespVO>> getTestOnePage(@Valid TestOnePageReqVO pageVO) {
        PageResult<TestOneDO> pageResult = testOneService.getTestOnePage(pageVO);
        return success(TestOneConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出第一个测试 Excel")
    @PreAuthorize("@ss.hasPermission('demo:test-one:export')")
    @OperateLog(type = EXPORT)
    public void exportTestOneExcel(@Valid TestOneExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<TestOneDO> list = testOneService.getTestOneList(exportReqVO);
        // 导出 Excel
        List<TestOneExcelVO> datas = TestOneConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "第一个测试.xls", "数据", TestOneExcelVO.class, datas);
    }

}
